﻿using System.Windows;
using AgFx;
using Coding4Fun.Phone.Controls.Data;
using DepSoft.Mobile.WP7.FollwIt.Framework;
using DepSoft.Mobile.WP7.FollwIt.Messages;
using GalaSoft.MvvmLight.Messaging;
using WP7Contrib.Services.Navigation;
using WP7Contrib.Services.Settings;
using WP7Contrib.Services.Storage;

namespace DepSoft.Mobile.WP7.FollwIt.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class TVShowSummaryViewModel : ViewModelBaseWp7
    {
        #region Public properties
        public string PageName
        {
            get
            {
                return "summary";
            }
        }
        #endregion

        #region Public bindable properties
        #region ApplicationTitle
        /// <summary>
        /// The <see cref="ApplicationTitle" /> property's name.
        /// </summary>
        public const string ApplicationTitlePropertyName = "ApplicationTitle";

        private string _applicationTitle = string.Empty;

        /// <summary>
        /// Gets the ApplicationTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string ApplicationTitle
        {
            get
            {
                return _applicationTitle;
            }

            set
            {
                this.SetPropertyAndNotify(ref _applicationTitle, value, ApplicationTitlePropertyName);
            }
        }
        #endregion

        #region ProgressIndicatorVisible
        /// <summary>
        /// The <see cref="ProgressIndicatorVisible" /> property's name.
        /// </summary>
        public const string ProgressIndicatorVisiblePropertyName = "ProgressIndicatorVisible";

        private bool _progressIndicatorVisible = false;

        /// <summary>
        /// Gets the ProgressIndicatorVisible property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool ProgressIndicatorVisible
        {
            get
            {
                return _progressIndicatorVisible;
            }

            set
            {
                this.SetPropertyAndNotify(ref _progressIndicatorVisible, value, ProgressIndicatorVisiblePropertyName);
            }
        }
        #endregion

        #region TVShowSummary
        /// <summary>
        /// The <see cref="TVShowSummary" /> property's name.
        /// </summary>
        public const string TVShowSummaryPropertyName = "TVShowSummary";

        private agfxTVShowSummaryViewModel _tvShowSummary = null;

        /// <summary>
        /// Gets the TVShowSummary property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public agfxTVShowSummaryViewModel TVShowSummary
        {
            get
            {
                return _tvShowSummary;
            }

            set
            {
                this.SetPropertyAndNotify(ref _tvShowSummary, value, TVShowSummaryPropertyName);
                this.ApplicationTitle = _tvShowSummary.TVShowDetail.Series_name;
            }
        }
        #endregion

        #region CoverVisible
        /// <summary>
        /// The <see cref="CoverVisible" /> property's name.
        /// </summary>
        public const string CoverVisiblePropertyName = "CoverVisible";

        private Visibility _coverVisible = Visibility.Collapsed;

        /// <summary>
        /// Gets the CoverVisible property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Visibility CoverVisible
        {
            get
            {
                return _coverVisible;
            }

            set
            {
                this.SetPropertyAndNotify(ref _coverVisible, value, CoverVisiblePropertyName);
            }
        }
        #endregion

        #region NoCoverVisible
        /// <summary>
        /// The <see cref="NoCoverVisible" /> property's name.
        /// </summary>
        public const string NoCoverVisiblePropertyName = "NoCoverVisible";

        private Visibility _noCoverVisible = Visibility.Visible;

        /// <summary>
        /// Gets the NoCoverVisible property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Visibility NoCoverVisible
        {
            get
            {
                return _noCoverVisible;
            }

            set
            {
                this.SetPropertyAndNotify(ref _noCoverVisible, value, NoCoverVisiblePropertyName);
            }
        }
        #endregion
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the TVShowSummaryViewModel class.
        /// </summary>
        public TVShowSummaryViewModel(INavigationService navigationService, IStorageService storageService, IStoreSettings settingsService, IMessenger messenger)
            : base(navigationService, storageService, settingsService, messenger)
        {
            //Use of the Coding 4 FUn PhoneHelper to get the application title from the configuration
            this.ApplicationTitle = PhoneHelper.GetAppAttribute("Title").ToUpper();
            this.MessengerInstance.Register<SelectedTVShowChangedMessage>(this, selectedTVShow => this.GetTVShowSummary(selectedTVShow.TVShowID));
        }
        #endregion

        #region Private methods
        private void GetTVShowSummary(int tvShowID)
        {
            this.ProgressIndicatorVisible = true;

            //Clear previous title
            this.ApplicationTitle = PhoneHelper.GetAppAttribute("Title").ToUpper();
            this.NoCoverVisible = Visibility.Visible;
            this.CoverVisible = Visibility.Collapsed;

            //First get the summary for the selected tv show
            DataManager.Current.Load<agfxTVShowSummaryViewModel>(tvShowID, (vm) =>
            {
                this.TVShowSummary = vm;
                this.CoverVisible = Visibility.Visible;
                this.NoCoverVisible = Visibility.Collapsed;
                this.ProgressIndicatorVisible = false;
            },
                (ex) =>
                {
                    this.ProgressIndicatorVisible = false;
                    throw new FollwitException("Error while loading TV Show Summary", ex);
                });

            //Secondly reference the seasons and episodes the user owns
        }
        #endregion

        #region ViewModelBaseWp7 implementation
        protected override void IsBeingActivated(WP7Contrib.Services.Storage.IStorage storage)
        {
            this.MessengerInstance.Send<SelectedTVShowChangedMessage>(new SelectedTVShowChangedMessage(storage.Read<int>("TVShowID")));
        }

        protected override void IsBeingDeactivated(WP7Contrib.Services.Storage.IStorage storage)
        {
            storage.Write<int>("TVShowID", int.Parse(_tvShowSummary.TVShowDetail.Follwit_series_id));
        }
        #endregion
    }
}